import Foundation
import UIKit
import SnapKit
import Alamofire
import RxSwift
import RxCocoa
class TaskGVView:UIView{
    //全局变量
    private var headImg = UIImageView(image: UIImage(named: "doTaskForUp.png"))
    private var remainingTaskView:RemainingTaskGVView = RemainingTaskGVView()
    public var listItems:GVCollection = GVCollection()
    private let api = "/growthvalue/tasklist"
    public var allHeight = 150
    public var allHeightLabel:UILabel = UILabel()
    
    //初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadData(userId: 1)
        attributeInit()
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //属性
    func attributeInit(){
        
    }
    //布局
    func setupUI(){
        
        
        self.addSubview(headImg)
        self.addSubview(remainingTaskView)
        self.addSubview(listItems)
        
        headImg.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.height.equalTo(50)
        }
        remainingTaskView.snp.makeConstraints { make in
            make.width.equalTo(327)
            make.height.equalTo(64)
            make.top.equalTo(headImg.snp.bottom).offset(16)
            make.centerX.equalToSuperview()
        }
        listItems.snp.makeConstraints { make in
            make.width.height.equalToSuperview()
            make.top.equalTo(remainingTaskView.snp.bottom).offset(20)
        }
        listItems.delegateCollection = self
    }
    //获取数据
    func loadData(userId:Int){
        let url = AllUrl.LH_URL + api + "?userId=\(userId)"
        AF.request(url,method: .get)
            .validate(statusCode: 200..<300)
            .responseDecodable(of:TaskInofGV.self){ [weak self] response in
                switch response.result{
                case .success(let taskList):
                    DispatchQueue.main.async {
                        self?.remainingTaskView.remainingTask.text =
                                                                "攻略：还差"
                                                                + String(taskList.remainingTaskNum)
                                                                + "个任务可赚10个资源解锁次数"
                        self?.remainingTaskView.reaminingNum = taskList.remainingTaskNum
                        self?.listItems.taskItems = taskList.taskInofItems
                        self?.allHeight += taskList.taskInofItems.count * self!.listItems.cellHeight
                        self?.delegateView?.reLoadPage()
                        self?.listItems.reloadData()
                    }
                case .failure(let error):
                    print("错误 -- GVCollection: \(error.localizedDescription)")
                }
            }
    }
    weak var delegateView: ViewDelegate?
    protocol ViewDelegate: AnyObject {
        func viewToController(text:String)
        func reLoadPage()
    }
}
extension TaskGVView: GVCollection.CollectionDelegate {
    func collectionToView(text:String) {
        delegateView?.viewToController(text: text)
    }
}
